package com.neuedu.music;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class MusicMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private static final Text MUSIC = new Text();
    private static final IntWritable ONE = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        //原始数据
        //1_song1_2016-1-11	song1	singer1	man	slow	pc
        //2_song2_2016-1-11	song2	singer2	woman	slow	ios
        //3_song3_2016-1-11	song3	singer3	man	quick	andriod
        //读取数据
        String line = value.toString();
        //为空判断
        if (StringUtils.isBlank(line)) {
            return;
        }
        //拆分数据
        String[] items = line.split("\t");
        //判断拆分前后数据是否一致
        if (items.length != 6) {
            return;
        }
        //输出：（歌曲，数量1）
        MUSIC.set(items[1]);
        context.write(MUSIC, ONE);
    }
}